package com.allwees.bs.core.model.coupon.entity;

import com.allwees.bs.core.model.coupon.constant.ECouponUseType;
import com.allwees.bs.core.model.coupon.constant.ETimeUnit;
import com.allwees.bs.core.model.user.constant.ECouponDefOverdueType;
import com.allwees.bs.core.model.user.constant.ECouponDefType;
import com.allwees.bs.core.modelbase.entity.BaseEntity;
import com.allwees.core.common.util.DateUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import java.math.BigDecimal;
import java.util.Date;

@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
@MappedSuperclass
public abstract class CouponDefModel extends BaseEntity {

    private static final long serialVersionUID = -7668376199168793810L;
    /** 名称 **/
    @Column(length = 128)
    protected String name;

//    /** 编号 **/
//    @Column(length = 128)
//    protected String no;

    /** 优惠券类型 **/
    @Column(length = 96)
    @Enumerated(EnumType.STRING)
    protected ECouponDefType type;

    /** 面额 **/
    protected BigDecimal faceValue;

    /** 逾期日期类型 **/
    @Column(length = 96)
    @Enumerated(EnumType.STRING)
    protected ECouponDefOverdueType overdueType;

    /** 固定日期的开始日期 **/
    @Temporal(TemporalType.DATE)
    protected Date fixedStartTime;

    /** 逾期值 **/
    protected Long overdueValue;

    /** 领取后过期时间参数 **/
    @Enumerated(EnumType.STRING)
    protected ETimeUnit overdueValueUnit;

    /** 发放数量 **/
    protected Integer count;

    /** 发放类型  **/
    @Column(length = 96)
    @Enumerated(EnumType.STRING)
    protected ECouponUseType useType;// 使用类型

    /** 单用户可领取数量 **/
    protected Integer singleUserReceiveMaxCount;

    /** 已领取数量 **/
    protected Integer receivedCount;

    /** 最大抵扣金额 **/
    protected BigDecimal maxDeductAmt;

    /** 值 text **/
    protected String valueText;

    /** 积分抵扣金额 **/
    protected BigDecimal pointDeductAmt;

    /**
     * 计算逾期日期
     * @return
     */
    public Date overdueTime(Date receiveDate) {
        Date date = ECouponDefOverdueType.FIXED.equals(overdueType) ? fixedStartTime : receiveDate;
        switch (overdueValueUnit) {
            case HOURS:
                return DateUtil.addHour(date, Integer.valueOf(String.valueOf(overdueValue)));
            case DAYS:
                return DateUtil.addDays(date, Integer.parseInt(String.valueOf(overdueValue)));
        }
        return null;
    }

    @Transient
    public Integer getReserve(){
        return count - receivedCount;
    }

}
